package edu.psu.collegerecommendationtool.coreservlets;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import edu.psu.collegerecommendationtool.model.AccountInfo;
import edu.psu.collegerecommendationtool.model.UserBean;


@ManagedBean(name="modifyAccount")
public class ModifyAccountInfoBean {
@ManagedProperty(value="#{userBean}")
   private UserBean user;

   private String entered_username;
   private String entered_password;
   private String entered_firstname;
   private String entered_lastname;
   private String entered_email;

   private AccountInfo accountInfo;

   public UserBean getUser() {
      return user;
   }
   public void setUser(UserBean user) {
      this.user = user;
   }

   public String getEntered_password() {
      return entered_password;
   }
   public void setEntered_password(String entered_password) {
      this.entered_password = entered_password;
   }

   public String getEntered_username() {
      return entered_username;
   }
   public void setEntered_username(String entered_username) {
      this.entered_username = entered_username;
   }

   public String getEntered_firstname() {
      return entered_firstname;
   }
   public void setEntered_firstname(String entered_firstname) {
      this.entered_firstname = entered_firstname;
   }

   public String getEntered_lastname() {
      return entered_lastname;
   }
   public void setEntered_lastname(String entered_lastname) {
      this.entered_lastname = entered_lastname;
   }

   public String getEntered_email() {
      return entered_email;
   }
   public void setEntered_email(String entered_email) {
      this.entered_email = entered_email;
   }

   public AccountInfo getAccountInfo() {
      return accountInfo;
   }
   public void setAccountInfo(AccountInfo accountInfo) {
      this.accountInfo = accountInfo;
   }

////////////////////////////////////////////////////////////////////////

   public String saveAccountInfo() {
      boolean action = false;
      accountInfo = user.getAccountInfo();

      if ((!entered_username.equals(accountInfo.getUsername())) &&
          (!entered_username.isEmpty())) {
         accountInfo.setUsername(entered_username);
         action = true;
      }

      if ((!entered_password.equals(accountInfo.getPassword())) &&
          (!entered_password.isEmpty())) {
         accountInfo.setPassword(entered_password);
         action = true;
      }

      if ((!entered_firstname.equals(accountInfo.getFirstname())) &&
          (!entered_firstname.isEmpty())) {
         accountInfo.setFirstname(entered_firstname);
         action = true;
      }

      if ((!entered_lastname.equals(accountInfo.getLastname())) &&
          (!entered_lastname.isEmpty())) {
         accountInfo.setLastname(entered_lastname);
         action = true;
      }
 
      if ((!entered_email.equals(accountInfo.getEmail())) &&
          (!entered_email.isEmpty())) {
         accountInfo.setEmail(entered_email);
         action = true;
      }
 

      if(true == action) {
         user.setAccountInfo(accountInfo);
         user.saveAccountInfo();
      }

      return ("ModifyInfoPage");
   }

}
